-- GuideSlimeAnimation
-- Created by zouyb
-- 引导冈布奥动画管理

-- GuideSlimeAnimation继承自Node
GuideSlimeAnimation = class("GuideSlimeAnimation", function()
    return cc.Node:create();
end);

function GuideSlimeAnimation.create(filePath)
    return GuideSlimeAnimation.new(filePath);
end

-- 构造函数
function GuideSlimeAnimation:ctor(filePath)
    -- 载入动画
    local node, action = AnimationCacheM.createAnimationNodeAndAction(filePath);

    self.node = node;
    self.action = action;
    self.filePath = filePath;

    node:runAction(action);
    self:addChild(node);

    self.hasDestroyed = false;

    -- 界面析构后，需要清理下
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            self:destroy();
        end
    end);
end

-----------------------------
-- 播放
-- @param actionType   动作类型
-- @param startIndex   起始帧
-- @param endIndex     结束帧
-- @param speed        播放速度
function GuideSlimeAnimation:play(loop, startIndex, endIndex, speed)
    if nil == loop then
        loop = false;
    end

    -- 播放
    self.action:setTimeSpeed(speed);
    self.action:gotoFrameAndPlay(startIndex, endIndex, loop);
end

-- 暂停播放
function GuideSlimeAnimation:pause()
    self.action:pause();
end

-- 销毁动画
function GuideSlimeAnimation:destroy()
    if not self.hasDestroyed then
        self:removeChild(self.node);
        AnimationCacheM.freeAnimationNodeAndAction(self.filePath, self.node, self.action);
        self.hasDestroyed = true;

        self.node = nil;
        self.action = nil;
        self.filePath = nil;
    end
end

